<?php
namespace app\center\controller;
use app\center\logic\CopyReplyLogic;

/**
 * 抄送批复api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/14
 * Time: 14:47
 */
class CopyReply extends Base {

    public function __construct(){
        parent::__construct();
        $this->cr_logic = new CopyReplyLogic();
    }

    /**
     * 获取抄送批复
     * @return \think\response\Json
     */
    public function ajax_get_cr(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->cr_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = $this->cr_logic->get_search_cr_maps($u_detail['openid'],$params['copy_openid'],$params['source'],$params['start_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('copy')->get_list($maps,'id,sourceid,source,name,status,is_read,is_reply_read,c_time','is_reply_read asc,id desc','',$limit,$params['rows']);

        $list_new = [];
        foreach($list as $k => $v){
            $temp_key = $v['sourceid'] . '_' . $v['source'];
            if(array_key_exists($temp_key,$list_new)){
                if($v['is_reply_read'] == 0){
                    $list_new[$temp_key]['is_reply_read'] = $v['is_reply_read'];
                }
            }else{
                $list_new[$temp_key] = $v;
            }
        }

        $data['list'] = array_values($list_new);
        $data['total_num'] = 0;
        $data['page'] = $params['page'];
        return json($this->cr_logic->get_response(1,'OK',$data));
    }
}
